package sz.szsmk.citizencard.ui.newversion.activity.presenter

import com.google.gson.Gson
import sz.szsmk.citizencard.DaggerApplication
import sz.szsmk.citizencard.model.network.bean.response.BaseBean
import sz.szsmk.citizencard.model.network.bean.response.CheckBankCodeRes
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean
import sz.szsmk.citizencard.model.network.dzbean.req.*
import sz.szsmk.citizencard.model.network.dzbean.res.GetBankNameRes
import sz.szsmk.citizencard.model.network.dzbean.res.GetOpenIdRes
import sz.szsmk.citizencard.model.network.dzbean.res.PreAuthResult
import sz.szsmk.citizencard.model.network.dzbean.res.SupportBankResponse
import sz.szsmk.citizencard.mvp.dzsmkpresenter.AppBasePresenter
import sz.szsmk.citizencard.retrofitrxjava.DZSmkAppInit
import sz.szsmk.citizencard.retrofitrxjava.newversion.ApiCallback
import sz.szsmk.citizencard.retrofitrxjava.newversion.HttpDateSource
import sz.szsmk.citizencard.retrofitrxjava.newversion.NewHttpApi
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource
import sz.szsmk.citizencard.ui.newversion.activity.view.BindBankCardMvpView
import sz.szsmk.citizencard.util.ACache
import sz.szsmk.citizencard.util.Constant
import sz.szsmk.citizencard.util.ObjectSignatureUtil
import sz.szsmk.citizencard.widget.GlobalVariables
import javax.inject.Inject

/**
 *  author : hyt
 *  date : 2020/10/19
 *  description :
 */
class BindBankCardPresenter@Inject constructor(var httpDateSourceNotUse: NetworkDateSource) :
        AppBasePresenter<BindBankCardMvpView>() {

    var openId = ""
    var getOpenIdRes:GetOpenIdRes?=null
    //请求openid
    fun getOpenId(access_token: String){
        val getOpenIdReq = GetOpenIdReq()
        getOpenIdReq.user_type = 0
        getOpenIdReq.access_token = access_token
        addSubscription(HttpDateSource.createApi()?.getOpenId(getOpenIdReq), object : ApiCallback<GetOpenIdRes?> {
            override fun onSuccess(response: GetOpenIdRes?) {
                response?.let {
                    getOpenIdRes = it
                    dzsmkAgreement(it.openId, "000003")
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    fun dzsmkAgreement(openid: String, channelID: String){
        val dzsmkAgreementReq = DzsmkAgreementReq()
        dzsmkAgreementReq.channelId = channelID
        dzsmkAgreementReq.openId = openid
        dzsmkAgreementReq.userType = 0

        addSubscription(HttpDateSource.createApi()?.dzsmkAgreement(dzsmkAgreementReq), object : ApiCallback<IbaseBean?> {
            override fun onSuccess(response: IbaseBean?) {
                response?.let {
                    preAuthThird()
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    fun preAuthThird(){
        val preAuthThiedReq = PreAuthThiedReq()

        preAuthThiedReq.mobileNumber = getOpenIdRes?.mobile
        preAuthThiedReq.openId = getOpenIdRes?.openId.toString()
        preAuthThiedReq.userId = getOpenIdRes?.userId.toString()
        preAuthThiedReq.photoUrl = ""
        preAuthThiedReq.channelId = "000003"

        ObjectSignatureUtil.signaturePreAuthRequest(preAuthThiedReq)

        addSubscription(HttpDateSource.createApi(NewHttpApi::class.java, Constant.DZ_BASE_URL)?.preAuthThird(preAuthThiedReq),
                object : ApiCallback<PreAuthThirdRes?> {
                    override fun onSuccess(response: PreAuthThirdRes?) {
                        response?.let {
                            val data: PreAuthThirdRes.DataBean = response.getData()
                            val json = Gson().toJson(data)
                            ACache.get(DaggerApplication.getIns()).put("token", response.getData().getToken())
                            val preAuthResult = Gson().fromJson(json, PreAuthResult::class.java)
                            GlobalVariables.getInstance(DZSmkAppInit.getInstance().context)
                                    .setPreAuthResult(DaggerApplication.getIns(), preAuthResult)
                            getSupportBank(openId)
                        }
                    }

                    override fun onFailure(model: BaseBean?, e: Throwable?) {
                        model?.let { mvpView.onFail(it) }
                    }
                })


    }

    //获取支持的银行
    fun getSupportBank(openid: String){
        var commonRequest = CommonRequest()
        commonRequest.openId = openid
        commonRequest.channelId = "000003"
        ObjectSignatureUtil.signatureRequest(commonRequest)
        addSubscription(HttpDateSource.createApi(NewHttpApi::class.java, Constant.DZ_BASE_URL)?.getSupportBankList(commonRequest), object : ApiCallback<SupportBankResponse?> {
            override fun onSuccess(response: SupportBankResponse?) {
                response?.let {
                    mvpView.getSupportBankSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    //验证银行卡
    fun checkBankCardNumberValidation(cardNo: String, selectBankCode: String){
        val checkBankCardRequset = CheckBankCardRequset()
        checkBankCardRequset.bankCardNo = cardNo
        checkBankCardRequset.bankbinCode = selectBankCode
        addSubscription(HttpDateSource.createApi(NewHttpApi::class.java, Constant.DZ_BASE_URL)
                ?.checkBankCardNumberValidation(checkBankCardRequset), object : ApiCallback<CheckBankCodeRes?> {
            override fun onSuccess(response: CheckBankCodeRes?) {
                response?.let {
                    mvpView.checkBankCardNumberValidationSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    // 验证银行卡后 继续获取银行
    fun getBankName(cardNo: String){
        val getBandCardNameReq = GetBankNameRequest()
        getBandCardNameReq.bankCardNumber = cardNo
        getBandCardNameReq.openId = openId
        ObjectSignatureUtil.signatureRequest(getBandCardNameReq)

        addSubscription(HttpDateSource.createApi(NewHttpApi::class.java, Constant.DZ_BASE_URL)
                ?.getBankName(getBandCardNameReq), object : ApiCallback<GetBankNameRes?> {
            override fun onSuccess(response: GetBankNameRes?) {
                response?.let {
                    mvpView.getBankNameSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })


    }


}